package com.yalantis.guillotine.sample.widget;

import com.yalantis.guillotine.sample.ResourceTable;
import com.yalantis.guillotine.sample.util.LogUtil;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Text;
import ohos.agp.text.Font;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.ResourceManager;
import ohos.global.resource.WrongTypeException;

import java.io.*;
import java.util.Optional;

public class CanaroTextView extends Text {

    private static final String TAG = CanaroTextView.class.getSimpleName();
    public static Font canaroExtraBold;

    public CanaroTextView(Context context) {
        this(context, null);
    }

    public CanaroTextView(Context context, AttrSet attrs) {
        this(context, attrs, "");
    }

    public CanaroTextView(Context context, AttrSet attrs, String styleName) {
        super(context, attrs, styleName);
        canaroExtraBold = getFont(getContext(), ResourceTable.String_canaro_extra_bold, 3, false);
        setFont(canaroExtraBold);
    }

    public static Font getFont(Context context, int fontId, int style, boolean italic) {
        final int BUFFER_LENGTH = 8192;
        final String RAW_FILE_PATH = "entry/resources/rawfile/";
        final int DEFAULT_ERROR = -1;
        String fontFamily = getString(context, fontId);
        String path = RAW_FILE_PATH + fontFamily;
        File file = new File(context.getDataDir(), fontFamily);

        try (OutputStream outputStream = new FileOutputStream(file);
             InputStream inputStream = context.getResourceManager().getRawFileEntry(path).openRawFile()) {
            byte[] buffer = new byte[BUFFER_LENGTH];
            int bytesRead = inputStream.read(buffer, 0, BUFFER_LENGTH);
            while (bytesRead != DEFAULT_ERROR) {
                outputStream.write(buffer, 0, bytesRead);
                bytesRead = inputStream.read(buffer, 0, BUFFER_LENGTH);
            }
        } catch (FileNotFoundException exception) {
            LogUtil.error(TAG, "loadFontFromFile -> FileNotFoundException : " + exception.getLocalizedMessage());
        } catch (IOException exception) {
            LogUtil.error(TAG, "loadFontFromFile -> IOException : " + exception.getLocalizedMessage());
        }
        return Optional.of(new Font.Builder(file).setWeight(style).makeItalic(italic).build()).get();
    }

    public static String getString(Context context, int id) {
        String result = "";
        if (context == null) {
            LogUtil.error(TAG, "getString -> get null context");
            return result;
        }
        ResourceManager manager = context.getResourceManager();
        if (manager == null) {
            LogUtil.error(TAG, "getString -> get null ResourceManager");
            return result;
        }
        try {
            result = manager.getElement(id).getString();
        } catch (IOException e) {
            LogUtil.error(TAG, "getString -> IOException");
        } catch (NotExistException e) {
            LogUtil.error(TAG, "getString -> NotExistException");
        } catch (WrongTypeException e) {
            LogUtil.error(TAG, "getString -> WrongTypeException");
        }
        return result;
    }
}
